package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.UpdateProjectActivity;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.vo.ProjectVO;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.button.shinebutton.ShineButton;
import com.xuexiang.xui.widget.progress.HorizontalProgressView;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

import cn.hutool.core.util.NumberUtil;

public class ListProjectAdapter extends RecyclerView.Adapter<ListProjectAdapter.ViewHolder> {

    private final Context mContext;
    private final List<ProjectVO> voList;
    private final Consumer<Boolean> consumer;
    private static final Type type = new TypeToken<Result<Boolean>>() {}.getType();


    public ListProjectAdapter(Context context, List<ProjectVO> voList, Consumer<Boolean> consumer) {
        this.mContext = context;
        this.voList = voList;
        this.consumer = consumer;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_project, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        holder.nameText.setText(voList.get(i).getName());
        if (voList.get(i).getAllCount() == 0) {
            holder.progress.setProgress(0);
        } else {
            BigDecimal a = NumberUtil.div(voList.get(i).getYesCount(), voList.get(i).getAllCount());
            holder.progress.setProgress((float) (a.doubleValue() * 100));
        }
        holder.progressText.setText(String.format(Locale.CHINA, "%d/%d", voList.get(i).getYesCount(), voList.get(i).getAllCount()));

        if (voList.get(i).getSort() == 1) {
            holder.starImg.setVisibility(View.VISIBLE);
            holder.starButton.setVisibility(View.GONE);
        }

        holder.starButton.setOnClickListener(v -> {
            String url = OkHttpUtils.BASE_URL + "/api/v1/project/set/top/" + voList.get(i).getId() + "/" + (voList.get(i).getSort() == 0 ? 1 : 0);
            voList.get(i).setSort(voList.get(i).getSort() == 0 ? 1 : 0);
            OkHttpUtils.getInstance().get(mContext, url, type, false, consumer);
        });

        holder.starImg.setOnClickListener(v -> {
            String url = OkHttpUtils.BASE_URL + "/api/v1/project/set/top/" + voList.get(i).getId() + "/" + (voList.get(i).getSort() == 0 ? 1 : 0);
            voList.get(i).setSort(voList.get(i).getSort() == 0 ? 1 : 0);
            holder.starImg.setVisibility(View.GONE);
            holder.starButton.setVisibility(View.VISIBLE);
            OkHttpUtils.getInstance().get(mContext, url, type, false, consumer);
        });

        holder.addNumText.setText(String.valueOf(voList.get(i).getTodayAddCount()));
        holder.finishNumText.setText(String.valueOf(voList.get(i).getTodayFinishCount()));
        holder.delayNumText.setText(String.valueOf(voList.get(i).getTodayDelayCount()));
        holder.mainLayout.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, UpdateProjectActivity.class);
            intent.putExtra("projectName", voList.get(i).getName());
            intent.putExtra("projectId", voList.get(i).getId());
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return voList.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView progressText;
        ShineButton starButton;
        HorizontalProgressView progress;
        TextView addNumText;
        TextView finishNumText;
        TextView delayNumText;
        ImageView starImg;
        ConstraintLayout mainLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.nameText = itemView.findViewById(R.id.name_text);
            this.progressText = itemView.findViewById(R.id.progress_text);
            this.starButton = itemView.findViewById(R.id.star_button);
            this.progress = itemView.findViewById(R.id.progress);
            this.addNumText = itemView.findViewById(R.id.add_num_text);
            this.finishNumText = itemView.findViewById(R.id.finish_num_text);
            this.delayNumText = itemView.findViewById(R.id.delay_num_text);
            this.starImg = itemView.findViewById(R.id.star_img);
            this.mainLayout = itemView.findViewById(R.id.main_layout);
        }
    }
}
